package aynu.hd.Hotel.Service.Impl;

import aynu.hd.Hotel.Dao.orderDao;
import aynu.hd.Hotel.Dao.roomDao;
import aynu.hd.Hotel.Dao.scheduleDao;
import aynu.hd.Hotel.Dao.userDao;
import aynu.hd.Hotel.Service.userService;
import aynu.hd.Login.LoginDao.loginDao;
import aynu.hd.pojo.Schedule;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
public class userServiceImpl implements userService {
    @Autowired
    userDao userDao;
    @Autowired
    roomDao roomDao;
    @Autowired
    scheduleDao scheduleDao;
    @Autowired
    loginDao loginDao;
    @Autowired
    orderDao orderDao;

    //收藏房间
    @Transactional
    @Override
    public String roomCollect(String id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        userDao.roomCollect(user.getUser_collect() + "@" + id, user.getUser_name());
        roomDao.addRoomcollect(id);
        user.setUser_collect(user.getUser_collect() + "@" + id);
        session.setAttribute("user", user);
        return "OK";
    }

    //取消收藏房间
    @Transactional
    @Override
    public String roomCollectOver(String id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        String[] splits = user.getUser_collect().split("@");
        String str = "0";
        for (int i = 1; i < splits.length; i++) {
            if (!splits[i].equals(id)) {
                str = str + "@" + splits[i];
            }
        }
        //用户收藏
        userDao.roomCollect(str, user.getUser_name());
        //房间收藏数量增加
        roomDao.reduceRoomcollect(id);
        user.setUser_collect(str);
        session.setAttribute("user", user);
        return "OK";
    }

    //预定房间
    @Transactional
    @Override
    public String roomReserve(String id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Schedule schedule = scheduleDao.queryScheduleByUser(user.getUser_id(), id);
        if (schedule != null) {
            return "ERROR";
        }
        //加入“购物车”
        scheduleDao.addScheduleRoom(user.getUser_id(), id);
        return "OK";
    }

    //取消预定房间
    @Transactional
    @Override
    public String removeSchedule(String schedule_id) {
        Schedule schedule = scheduleDao.queryScheduleById(schedule_id);
        if(schedule.getOrders()!=null){
            orderDao.deleteOrderById(schedule.getOrders().getOrders_id());
        }
        scheduleDao.removeSchedule(schedule_id);
        return "OK";
    }

    //付款界面
    @Transactional
    @Override
    public Schedule addRoomOrder(String schedule_id) {
        return scheduleDao.queryScheduleById(schedule_id);
    }

    //更新个人信息
    @Transactional
    @Override
    public String userUpdate(User user, HttpSession session) {
        User user1 = loginDao.queryUserByName(user.getUser_name());
        User user2 = (User) session.getAttribute("user");
        if(user1!=null && !user1.getUser_name().equals(user2.getUser_name())){
            return "ERROR";
        }
        User user3 = loginDao.queryUserByEmail(user.getUser_email());
        if(user3!=null && !user3.getUser_email().equals(user2.getUser_email())){
            return "FAIL";
        }
        User user4 = loginDao.queryUserByIdentityCard(user.getUser_identity_card());
        if(user4!=null && !user4.getUser_identity_card().equals(user2.getUser_identity_card())){
            return "CARD";
        }
        user.setUser_id(user2.getUser_id());
        userDao.userUpdate(user);
        session.removeAttribute("user");
        return "OK";
    }
}
